package com.hbwl.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.PageResult;
import com.hbwl.dao.UserDao;
import com.hbwl.dto.User;

import java.sql.SQLException;
import java.util.Map;
import java.util.Set;

public class UserDaoImpl implements UserDao {
    @Override
    public boolean save(User user) throws SQLException {
        int insert = Db.use().insert(
                Entity.create("tb_user")
                        .set("uname", user.getUname())
                        .set("upwd", user.getUpwd())
                        .set("remark", user.getRemark())

        );
        return insert>0;

    }

    @Override
    public PageResult<Entity> queryByPage(int pageindex, int pagesize, Map<String, Object> searchMap) throws SQLException {
        //查询条件
        Entity tb_user = Entity.create("tb_user");
        if(searchMap!=null&&searchMap.size()>0)
        {
            Set<Map.Entry<String, Object>> entries = searchMap.entrySet();
            for(Map.Entry<String, Object> entry:entries)
            {
                String key=entry.getKey();
                Object value=entry.getValue();
                tb_user.set(key,value);
            }

        }
        //分页查询
        PageResult<Entity> page = Db.use().page(
                tb_user, pageindex, pagesize);


        return page;
    }


    public static void main(String[] args) throws SQLException {

        PageResult<Entity> entities = new UserDaoImpl().queryByPage(0, 4, null);
        System.out.println(entities.getTotal());   //共多少条数据
        System.out.println(entities.getPageSize()); //共多少页
        System.out.println(entities.getPage());  //当前是第几页
        System.out.println(entities.getTotalPage()); //每页的数据条目数

        for (int i=0;i< entities.size();i++)
        {
            System.out.println(entities.get(i));
        }
    }
    
    
}
